class Story < ActiveRecord::Base

  require File.dirname(__FILE__) + '/../../lib/lingua/en/readability'

  acts_as_rateable

  has_many :lines
  has_and_belongs_to_many :tags
  has_many :comments
  belongs_to :user
  
  validates_presence_of :title
  validates_length_of   :title,           :maximum => 100,  :allow_blank => true
  
  validates_presence_of :user
  validates_associated  :user
  
  validates_presence_of :typ
  validates_presence_of :rating

  # add a field "type"

  def check_restrictions(txt)
    if(self.typ.eql?("Limerick"))
      if (self.lines.length == 0)
        syllablesA = numSyllables(txt)
        if(syllablesA == 7 || syllablesA== 8 ||  syllablesA == 9)
          return [true, 'Success']
        else
          return [false, 'The first line should have 7, 8, or 9 syllables. You have ' + syllablesA.to_s]
        end
      elsif (self.lines.length == 1)
        if (! rhyme?(lines[0].line.split("\ ")[-1].gsub(/[^a-zA-Z]/, '') , txt.split("\ ")[-1].gsub(/[^a-zA-Z]/, '')))
          return [false, 'Check your rhyming again!']
        end
        rslt = sameSyllables(self.lines[0].line, txt)
        if (!rslt[0])
          return [false, 'Check your syllables again! ' + 'You need ' + rslt[1].to_s + ' syllables but have ' + rslt[2].to_s]
        end
        return [true, 'Success']
      elsif (self.lines.length  == 2)
        syllablesB = numSyllables(txt)
        if(syllablesB == 5 || syllablesB == 6)
          return [true, 'Success']
        else
          return [false, 'The second line should have 5 or 6 syllables. You have ' + syllablesA.to_s]
        end
      elsif (self.lines.length == 3)
        if (! rhyme?(lines[2].line.split("\ ")[-1].gsub(/[^a-zA-Z]/, '') , txt.split("\ ")[-1].gsub(/[^a-zA-Z]/, '')))
          return [false, 'Check your rhyming again!']
        end
        rslt = sameSyllables(self.lines[2].line, txt)
        if (!rslt[0])
          return [false, 'Check your syllables again! ' + 'You need ' + rslt[1].to_s + ' syllables but have ' + rslt[2].to_s]
        end
        return [true, 'Success']
      elsif (self.lines.length == 4)
        if (! rhyme?(lines[0].line.split("\ ")[-1].gsub(/[^a-zA-Z]/, '') , txt.split("\ ")[-1].gsub(/[^a-zA-Z]/, '')))
          return [false, 'Check your rhyming again!']
        end
        rslt = sameSyllables(self.lines[0].line, txt)
        if (!rslt[0])
          return [false, 'Check your syllables again! ' + 'You need ' + rslt[1].to_s + ' syllables but have ' + rslt[2].to_s]
        end
        return [true, 'Success']
      else
        return [false, 'This limerick is already complete']
      end
    elsif (self.typ.eql?("Haiku"))
      if (self.lines.length == 0)
        syllablesA = numSyllables(txt)
        if(syllablesA == 5)
          return [true, 'Success']
        else
          return [false, 'The first line of a Haiku should have 5 syllables. You have ' + syllablesA.to_s]
        end
      elsif (self.lines.length == 1)
        syllablesA = numSyllables(txt)
        if(syllablesA == 7)
          return [true, 'Success']
        else
          return [false, 'The second line of a Haiku should have 7 syllables. You have ' + syllablesA.to_s]
        end
      elsif (self.lines.length == 2)
        syllablesA = numSyllables(txt)
        if(syllablesA == 5)
          return [true, 'Success']
        else
          return [false, 'The third line of a Haiku should have 5 syllables. You have ' + syllablesA.to_s]
        end
      else
        return [false, 'This Haiku is completed']
      end
    else
      return [true, 'Success']
    end
  end
  
  protected
  
  def rhyme?(strA, strB)
      require 'gdbm'
      wd = GDBM.open(File.dirname(__FILE__) + '/../../lib/rhymedb/words.db')
      rh = GDBM.open(File.dirname(__FILE__) + '/../../lib/rhymedb/rhymes.db')
      strA.upcase!
      strB.upcase!
      tmp = wd[strA].split("\ ")[0]
      rh[tmp]
      to_rtn = rh[tmp].split("\ ").member?(strB)
      wd.close()
      rh.close()
      return to_rtn
  end


  def numSyllables(str)
      report = Lingua::EN::Readability.new(str)
      report.num_syllables
  end


  def sameSyllables(strA, strB)
      reportA = Lingua::EN::Readability.new(strA)
      reportB = Lingua::EN::Readability.new(strB)
      sylla_a = reportA.num_syllables
      sylla_b = reportB.num_syllables
    return [sylla_a == sylla_b, sylla_a, sylla_b]
  end
  
end
